/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.helloswing;

import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Group;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

/**
 *
 * @author winnie
 */
public class CelsiusConverter extends JFrame {
    private JTextField tempTextField;
    private JLabel celsiusLabel;
    private JLabel fahrenheitLabel;
    private JButton convertButton;

    public CelsiusConverter() throws HeadlessException {
        initComponents();
    }

    private void initComponents() {
        tempTextField = new JTextField();
        celsiusLabel = new JLabel("Celsius");
        fahrenheitLabel = new JLabel("Fahrenheit");
        convertButton = new JButton("Convert");
        
//        add(convertButton);
        this.setTitle("Celsius Converter");
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        
        convertButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                doConvert(ae);
            }
        });
        
        GroupLayout layout = new GroupLayout(this.getContentPane());
        this.getContentPane().setLayout(layout);
        
        //details of the layout
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(
                layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(
                    layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addGroup(
                        layout.createSequentialGroup()
                        .addComponent(tempTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(ComponentPlacement.RELATED)
                        .addComponent(celsiusLabel)
                    )
                    .addGroup(
                        layout.createSequentialGroup()
                        .addComponent(convertButton, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(ComponentPlacement.RELATED)
                        .addComponent(fahrenheitLabel)
                    )
                )
                .addContainerGap(27, Short.MAX_VALUE)
            )
        );
        
        layout.linkSize(SwingConstants.HORIZONTAL, convertButton, tempTextField);
        
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(
                layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(
                    layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(tempTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addComponent(celsiusLabel)
                )
                .addPreferredGap(ComponentPlacement.RELATED)
                .addGroup(
                    layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(convertButton, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addComponent(fahrenheitLabel)
                )
                .addContainerGap(21, Short.MAX_VALUE)
            )
        );
        
        pack();
    }
    
    private void doConvert(ActionEvent ae) {
//        System.out.println("Start Converting");
        int tempFahr = (int)((Double.parseDouble(tempTextField.getText()))
            * 1.8 + 32);
        fahrenheitLabel.setText(tempFahr + " Fahrenheit");
    }
}
